export class Socket {
  wSocket: WebSocket;
  setIntervalWesocketPush = null;
  URL = '';

  /**
   * 建立websocket连接
   * @param {string} url ws地址
   */
  createSocket(url?) {
    console.log('连接', url);
    if (url) {
      this.URL = url;
    }
    this.wSocket && this.wSocket.close();
    if (!this.wSocket) {
      if (this.URL) {
        console.log('建立websocket连接');
        this.wSocket = new WebSocket(this.URL);
        this.wSocket.onopen = this.onopenWS;
        this.wSocket.onmessage = this.onmessageWS;
        this.wSocket.onerror = this.onerrorWS;
        this.wSocket.onclose = this.oncloseWS;
      }
    } else {
      console.log('websocket已连接');
    }
  }
  /** 打开WS之后发送心跳 */
  onopenWS = () => {
    console.log('连接成功');

    this.sendPing();
  };

  /** 连接失败重连 */
  onerrorWS = () => {
    this.wSocket.close();
    clearInterval(this.setIntervalWesocketPush);
    console.log('连接失败重连中', this.setIntervalWesocketPush);
    console.log(this.wSocket.readyState, 'Socket.readyState');
    if (this.wSocket.readyState !== 3) {
      this.wSocket = null;
      this.createSocket();
    }
  };
  /** WS数据接收统一处理 */
  onmessageWS = e => {
    console.log(e, 'eee==============>>>>>>>>>>>>>>>>>');
    window.dispatchEvent(new CustomEvent('onmessageWS', { detail: { data: e.data } }));
  };

  /**
   * 发送数据但连接未建立时进行处理等待重发
   * @param {any} message 需要发送的数据
   */
  connecting = message => {
    console.log('connecting', message);
    setTimeout(() => {
      if (this.wSocket.readyState === 0) {
        this.connecting(message);
      } else {
        this.wSocket.send(message);
      }
    }, 1000);
  };

  /**
   * 发送数据
   * @param {any} message 需要发送的数据
   */
  sendWSPush = message => {
    console.log(message, 'sendWSPush');
    if (this.wSocket !== null && this.wSocket.readyState === 3) {
      this.wSocket.close();
      this.createSocket();
    } else if (this.wSocket.readyState === 1) {
      this.wSocket.send(message);
    } else if (this.wSocket.readyState === 0) {
      this.connecting(message);
    }
  };
  /** 断开重连 */
  oncloseWS = () => {
    console.log('断开重连', 'oncloseWS', this.wSocket.readyState);
    clearInterval(this.setIntervalWesocketPush);
    console.log(this.setIntervalWesocketPush, 'this.setIntervalWesocketPush');
    if (this.wSocket.readyState !== 2) {
      this.wSocket = null;
      setTimeout(() => {
        this.createSocket();
      }, 5000);
    }
  };
  /** 发送心跳
   * @param {number} time 心跳间隔毫秒 默认5000
   * @param {string} ping 心跳名称 默认字符串ping
   */
  sendPing = (time = 5000, ping = 'ping') => {
    console.log('sendPing', time, this.setIntervalWesocketPush);
    clearInterval(this.setIntervalWesocketPush);
    this.wSocket.send(ping);
    this.setIntervalWesocketPush = setInterval(() => {
      this.wSocket.send(ping);
    }, time);
  };

  closeWS = () => {
    console.log('中断', this.wSocket.close);
    this.wSocket.close && this.wSocket.close();
    this.wSocket = null;
    clearInterval(this.setIntervalWesocketPush);
    this.URL = null;
  };
}
